package com.kkb.dao.impl;

import com.kkb.bean.User;
import com.kkb.dao.UserDao;
import com.kkb.enumeration.StudentState;
import com.kkb.util.DBUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * 用户dao实现
 */
public class UserDaoImpl implements UserDao {
    @Override
    public User findByLoginNameAndPassword(String loginName, String password) {
        String sql = "select * from t_user where loginname = ? and password = ?";
        User user = null;
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            conn = DBUtils.getConnection();
            ps = conn.prepareStatement(sql);
            ps.setString(1, loginName);
            ps.setString(2, password);
            rs = ps.executeQuery();
            while (rs.next()) {
                user = new User();
                user.setId(rs.getInt("id"));
                user.setLoginName(rs.getString("loginname"));
                user.setRealName(rs.getString("realname"));
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            DBUtils.close(conn, ps, rs);
        }
        return user;
    }
}
